package com.filldream.fastboot.common.okutil.stockIndex;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.common.stockUtils.qt.entity.StockInfo;
import com.filldream.fastboot.common.stockUtils.qt.StockUtil;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.util.List;

/**
 * 指数工具类
 */
@Slf4j
public class StockIndexUtil {

    public static void main(String[] args) {

    }

    /**
     * 获取美国十年期国债收益率-指数
     * @return
     */
    public static BigDecimal getUS10Y(){
        try {
            String json = HttpUtil.get("https://api-ddc-wscn.awtmt.com/market/kline?prod_code=US10YR.OTC&tick_count=1&period_type=300&adjust_price_type=forward&fields=tick_at%2Copen_px%2Cclose_px%2Chigh_px%2Clow_px%2Cturnover_volume%2Cturnover_value%2Caverage_px%2Cpx_change%2Cpx_change_rate%2Cavg_px%2Cma2");
            JSONObject httpResultObj = JSONObject.parseObject(json);
            Integer code = httpResultObj.getInteger("code");
            if (code.equals(20000)) {
                JSONArray lines = httpResultObj.getJSONObject("data").getJSONObject("candle").getJSONObject("US10YR.OTC").getJSONArray("lines");
                JSONArray lineObj = lines.getJSONArray(0);
                BigDecimal bigDecimal = lineObj.getBigDecimal(0);
                return bigDecimal;
            }
        }catch (Exception e){
            log.error("获取US10Y指数失败", e);
        }
        return null;
    }

    /**
     * 获取中国30年期国债收益率指数
     * @return
     */
    public static BigDecimal getCN30Y(){
        try {
            String json = HttpUtil.get("https://api-ddc-wscn.awtmt.com/market/kline?prod_code=CN30YR.OTC&tick_count=1&period_type=300&adjust_price_type=forward&fields=tick_at%2Copen_px%2Cclose_px%2Chigh_px%2Clow_px%2Cturnover_volume%2Cturnover_value%2Caverage_px%2Cpx_change%2Cpx_change_rate%2Cavg_px%2Cma2");
            JSONObject httpResultObj = JSONObject.parseObject(json);
            Integer code = httpResultObj.getInteger("code");
            if (code.equals(20000)) {
                JSONArray lines = httpResultObj.getJSONObject("data").getJSONObject("candle").getJSONObject("CN30YR.OTC").getJSONArray("lines");
                JSONArray lineObj = lines.getJSONArray(0);
                BigDecimal bigDecimal = lineObj.getBigDecimal(0);
                return bigDecimal;
            }
        }catch (Exception e){
            log.error("获取US10Y指数失败", e);
        }
        return null;
    }

    /**
     * 获取美元黄金
     * @return
     */
    public static String getUsua(){
        try {
            String json = cn.hutool.http.HttpUtil.get("https://finance.pae.baidu.com/vapi/v1/getquotation?all=1&srcid=5353&pointType=string&group=quotation_minute_us&market_type=us&new_Format=1&finClientType=pc&query=USAU&code=USAU");
            JSONObject httpResultObj = JSONObject.parseObject(json);
            Integer resultCode = httpResultObj.getInteger("ResultCode");
            if (resultCode == 0) {
                String curObj = httpResultObj.getJSONObject("Result").getJSONObject("cur").getString("price");
                return curObj;
            }
        }catch (Exception e){
            log.error("获取美元黄金失败", e);
        }
        return null;
    }


    /**
     * 获取自定义汇率
     * @param code
     * @return
     */
    public static JSONObject getDiyHL(String code){
        try{
            String json = cn.hutool.http.HttpUtil.get("https://finance.pae.baidu.com/vapi/v1/getquotation?group=huilv_minute&need_reverse_real=1&code="+code+"&finClientType=pc");
            JSONObject httpResultObj = JSONObject.parseObject(json);
            Integer resultCode = httpResultObj.getInteger("ResultCode");
            if (resultCode == 0) {
                JSONObject curObj = httpResultObj.getJSONObject("Result").getJSONObject("cur");
                return curObj;
            }
        }catch (Exception e){
            log.error("获取自定义汇率{}失败", code, e);
        }
        return null;
    }

    /**
     * 获取A股两市市值
     * @return
     */
    public static BigDecimal getAMarketPrice(){
        BigDecimal sum = BigDecimal.ZERO;
        try {
            List<StockInfo> list = StockUtil.getByCodes("sh000001,sz399001");
            for (StockInfo stockInfo : list) {
                sum = sum.add(stockInfo.getMarketPrice());
            }
            return sum.divide(new BigDecimal("10000")).setScale(4, BigDecimal.ROUND_DOWN);
        }catch (Exception e){
            log.error("获取A股两市市值失败", e);
        }
        return sum;
    }

    /**
     * 获取A50期指当月连续
     * https://waptest.eastmoney.com/quote/stock/104.CN00Y.html
     * @return
     */
    public static JSONObject getA50KeepDate(){
        try {
            String json = HttpUtil.get("https://push2.eastmoney.com/api/qt/stock/get?wbp2u=|0|0|0|wap&secid=104.CN00Y&forcect=1&fields=f43,f169,f170,f58,f44,f45,f46&ut=f057cbcbce2a86e2866ab8877db1d059&fltt=1&invt=2&dect=0");
            JSONObject httpResultObj = JSONObject.parseObject(json);
            if (httpResultObj.containsKey("data")) {
                JSONObject data = httpResultObj.getJSONObject("data");
                return data;
            }
        }catch (Exception e){
            log.error("获取A50期指当月连续失败", e);
        }
        return null;
    }

}
